<template>
  <uni-popup ref="exampleModal" type="center" :mask-click="false">
    <view class="example-modal">
      <view class="example-header">
        <text class="example-title">充值凭证示例</text>
        <text class="close-btn" @click="handleClose">×</text>
      </view>
      <view class="example-image-container">
        <img 
          :src="imageSrc" 
          class="example-image"
          @error="onImageError"
          @load="onImageLoad"
          alt="示例图片"
        />
        <text v-if="imageError" class="error-text">图片加载失败: {{ imageSrc }}</text>
      </view>
    </view>
  </uni-popup>
</template>

<script>
import shilituImage from '/static/Images/common/Shilitu.png'
import { safeCallRefMethod, safeNextTick, safeDelayedCall } from '@/utils/vue3-compat'

export default {
  name: 'ExampleModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    imageSrc: {
      type: String,
      default: shilituImage
    }
  },
  emits: ['close'],
  data() {
    return {
      imageError: false
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        safeNextTick(() => {
          safeDelayedCall(() => {
            safeCallRefMethod('exampleModal', 'open', [], this)
          }, 100, this)
        })
      } else {
        safeCallRefMethod('exampleModal', 'close', [], this)
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('close');
    },
    onImageError(e) {
      console.error('图片加载失败:', this.imageSrc, e);
      this.imageError = true;
    },
    onImageLoad(e) {
      console.log('图片加载成功:', this.imageSrc, e);
      this.imageError = false;
    }
  }
};
</script>

<style scoped>
.example-modal {
  width: 320px;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  z-index: 999;
  margin: 0 auto;
}

.example-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.example-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  font-size: 24px;
  color: #999;
  cursor: pointer;
  line-height: 1;
}

.example-image-container {
  padding: 20px;
  text-align: center;
}

.example-image {
  width: 100%;
  max-width: 280px;
  height: auto;
  border-radius: 8px;
}

.error-text {
  color: #ff4d4f;
  font-size: 14px;
  margin-top: 10px;
  display: block;
}
</style>